<?php
/*
 * @Author: 哲学家乔布斯 <wuyangii@qq.com>
 * @Date: 2022-10-20 21:41:37
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-11-07 22:37:15
 * @Descripttion: 首页
 */

namespace app\api\controller\user\v1_0;

use Workerman\Events\Select;

class Cate extends Common
{
    protected $noNeedRight = ['test'];

    public function index(){
        $list = (new \app\common\model\GoodsCate())
                ->where(['pid' => 0,'is_display' => 1,'is_del' => 0])
                ->append(['list'])
                ->field(['id','pid','title'])
                ->order('sort desc')
                ->withAttr('list',function($value,$data){
                    return (new \app\common\model\GoodsCate())
                            ->where(['pid' => $data['id'],'is_display' => 1 ,'is_del' => 0])
                            ->field(['id','pid','title'])
                            ->order('sort desc')
                            ->select();
                })
                ->select();
        return self::successResult("成功",$list);
    }
    
    public function goods_list(){
        $page = empty(request()->param('page')) ? 1 : request()->param('page');
        $limit = empty(request()->param('limit')) ? 10 : request()->param('limit');
        $user = empty($this->UserInfo) ? '' : (new \app\common\model\User())->where(['id' => $this->UserInfo['id']])->find();
        $id = request()->param('id');
        $where = [];
        if(!empty($id)){
            $cate = (new \app\common\model\GoodsCate())->where(['id' => $id , 'pid' => 0])->find();
            if($cate){
                $cate_id = (new \app\common\model\GoodsCate())->where(['pid' => $id])->column('id');
                $where['goods.cate_id'] = $cate_id;
            }else{
                $where['goods.cate_id'] = $id;
            }
            
        }
        $bus_id = $this->bus_id;
        $agent_id = $this->agent_id;
        $goods_list = (new \app\common\model\AgentGoodsSku())
                    ->alias("agent_goods_sku")
                    ->join("{$this->tdb}goods goods","goods.id = agent_goods_sku.goods_id")
                    ->field([
                        'agent_goods_sku.id',
                        'goods.img',
                        'goods.title',
                        'goods.introduce',
                        'agent_goods_sku.money',
                        'agent_goods_sku.money_primary', 
                        'agent_goods_sku.stock',
                        'agent_goods_sku.team_rebate',
                        ])
                    ->where(['agent_goods_sku.agent_id' => $this->agent_id])
                    ->where($where)
                    ->where([
                        'agent_goods_sku.is_display' => 1,
                        'goods.is_display' => 1,
                        'agent_goods_sku.is_del' => 0,
                        'goods.is_del' => 0,
                    ])
                    ->append(['img','cart_num'])
                    ->withAttr("cart_num",function($value,$data) use($user,$agent_id,$bus_id){
                        return empty($user)? 0 : (new \app\common\model\AgentGoodsCart())->where([
                            'goods_sku_id' => $data['id'],
                            'user_id' => $user['id'],
                            'agent_id' => $agent_id,
                            'bus_id' => $bus_id,
                            ])->sum('num');
                    })
                    ->page($page)
                    ->limit($limit)
                    ->order('agent_goods_sku.sort desc ,agent_goods_sku.id desc')
                    ->select();
        return self::successResult("成功",$goods_list);
    }
    
}
